/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.projetoObras.view;

import br.com.projetoObras.controller.ArtistController;
import br.com.projetoObras.model.Artist;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author viniciusvdias
 */
public class ArtistSearchView extends javax.swing.JPanel {

    /**
     * Creates new form ArtistSerachView
     */
    private ArtistController artist_controller;
    private MainFrame m_frame;
    private ArtistRegistryView m_artist;
    List<Artist> artists;

    public ArtistSearchView(MainFrame mf, ArtistController ac) throws SQLException {
        initComponents();
        this.m_frame = mf;
        this.tabela_result.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    JTable target = (JTable) e.getSource();
                    int row = target.getSelectedRow();

                    criaArtistView();

                    m_artist.getBtn_editar().setEnabled(true);
                    m_artist.getBtn_excluir().setEnabled(false);
                    m_artist.getTxt_status().setText(null);

                    m_artist.setArtist(artists.get(row));
                    artist_controller.atualizaArtistView(m_artist, m_artist.getArtist());
                }

            }
        });
        this.artist_controller = ac;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txt_string_busca = new javax.swing.JTextField();
        btn_buscar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabela_result = new javax.swing.JTable();
        txt_status = new javax.swing.JLabel();

        jLabel1.setText("Nome/Sobrenome");

        btn_buscar.setText("Buscar");
        btn_buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_buscarActionPerformed(evt);
            }
        });

        tabela_result.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tabela_result);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txt_string_busca, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btn_buscar)))
                .addContainerGap())
            .addComponent(txt_status, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txt_string_busca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_buscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 309, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(txt_status, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btn_buscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_buscarActionPerformed

        tabela_result.setEnabled(true);

        String search_str = this.txt_string_busca.getText();
        artists = null;
        int i, size;
        try {
            artists = this.artist_controller.getArtists(search_str);
            size = artists.size();
            String[][] data = new String[size][1];
            for (i = 0; i < size; i++) {
                data[i][0] = artists.get(i).getNome();
            }
            this.tabela_result.setModel(new DefaultTableModel(data,
                    new String[]{"Artistas"}));

            txt_status.setText("Pesquisa realizada, verifique os resultados na tabela acima ..");

        } catch (IOException ex) {
            Logger.getLogger(ArtistSearchView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ArtistSearchView.class.getName()).log(Level.SEVERE, null, ex);
            txt_status.setText("Erro na pesquisa ..");
        }
    }//GEN-LAST:event_btn_buscarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_buscar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabela_result;
    private javax.swing.JLabel txt_status;
    private javax.swing.JTextField txt_string_busca;
    // End of variables declaration//GEN-END:variables

    public JTable getTabela_result() {
        return tabela_result;
    }

    private void criaArtistView() {
        if (m_artist == null) {

            try {
                m_artist = new ArtistRegistryView(artist_controller, this, m_frame);
            } catch (SQLException ex) {
                Logger.getLogger(ArtistSearchView.class.getName()).log(Level.SEVERE, null, ex);
            }
            m_frame.getMain_panel().add(m_artist, BorderLayout.LINE_START);
            m_frame.repaint();
            m_frame.show();
        }
        if (m_artist.getFoto_artista() != null) {
            m_artist.getFoto_artista().setVisible(false);
        }

        m_artist.setVisible(true);
    }

    public JLabel getTxt_status() {
        return txt_status;
    }

    public void setTxt_status(JLabel txt_status) {
        this.txt_status = txt_status;
    }
    
    
}
